﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ActUtlTypeLib;

namespace Commonality.Model
{
    public class MitsubisiPlcModel
    {
        public static List<ActUtlTypeClass> MitsubisiPLC = new List<ActUtlTypeClass>();
        public static void MitsubisiPlcModelIni(int Offset, int Address)
        {
            MitsubisiPLC = new List<ActUtlTypeClass>(Offset+ 1);
            for (int i = 0; i < MitsubisiPLC.Capacity; i++)
            {
                MitsubisiPLC.Add(new ActUtlTypeClass());
            }
            for (int i = 0; i < MitsubisiPLC.Count; i++)
            {
                MitsubisiPLC[i].ActLogicalStationNumber = i + Address;
            }      
        }
        #region 读取
        //public static bool ReadBit(ushort stationNumber, string device)
        //{
        //    int data = 0x00;
        //    int str = MitsubisiPLC[stationNumber - 1].ReadDeviceRandom(device, 1, out data);
        //    return data > 0 ? true : false;
        //}
        public static byte ReadBit(ushort stationNumber, string device)
        {
            int data = 0x00;
            int str = MitsubisiPLC[stationNumber - 1].ReadDeviceRandom(device, 1, out data);
            return (byte)data;
        }
        public static Int16 ReadBytes(ushort stationNumber, string device)
        {
            int data = 0x00;
            int str = MitsubisiPLC[stationNumber - 1].ReadDeviceRandom(device, 1, out data);
            return Convert.ToInt16(data);
        }
        public static Int32 ReadDoubleBytes(ushort stationNumber, string device)
        {
            int data = 0x0000;
            int str = MitsubisiPLC[stationNumber - 1].ReadDeviceRandom(device, 2, out data);
            return Convert.ToInt16(data);
        }
        #endregion
        #region 写入
        public static void WriteBit(ushort stationNumber, string device,int data)
        {
            int str = MitsubisiPLC[stationNumber - 1].WriteDeviceRandom(device, 1, ref data);
        }
        public static void WriteBytes(ushort stationNumber, string device, int data)
        {
            int str = MitsubisiPLC[stationNumber - 1].WriteDeviceRandom(device, 1, ref data);
        }
        public static void WriteDoubleBytes(ushort stationNumber, string device, int data)
        {
            int str = MitsubisiPLC[stationNumber - 1].WriteDeviceRandom(device, 2, ref data);
        }
        #endregion
        public static string Open(ushort StationNumber)
        {
            string ret = "";
            switch (MitsubisiPLC[StationNumber - 1].Open())
            {
                case 0:
                    ret = "";
                    break;
                case 1:
                    ret = "";
                    break;
                case 2:
                    ret = "";
                    break;
                default:
                    break;
            }
            return ret;
        }
        public static string Close(ushort StationNumber)
        {
            string ret = "";
            switch (MitsubisiPLC[StationNumber - 1].Close())
            {
                case 0:
                    ret = "";
                    break;
                case 1:
                    ret = "";
                    break;
                case 2:
                    ret = "";
                    break;
                default:
                    break;
            }
            return ret;
        }
    }
}
